package cn.wit.summit.chwan.widgets;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.tools.downloadtool.DownLoadStatusConfig;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.APKUtils;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.UtilsMy;

public class GameDetialDownButn extends DownloadViewStrokeBig {
    CustomProgressBar progressBar;
    TextView energyNumber;
    TextView moneyText;
    TextView instalButtomTwoButn;
    LinearLayout energylayout;

    private int formId = 0;

    public GameDetialDownButn(Context context) {
        super(context);
    }

    public GameDetialDownButn(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GameDetialDownButn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    void initView(Context context) {
        isDetial=true;
        View view = LayoutInflater.from(context).inflate(R.layout.gamedetail_downbutn_layout, this);
        moneyText = (TextView) view.findViewById(R.id.moneyText);
        instalButtomButn = (TextView) view.findViewById(R.id.instalButtomButn);
        instalButtomTwoButn = (TextView) view.findViewById(R.id.instalButtomTwoButn);
        progressBar = view.findViewById(R.id.progressBar);
        energyNumber = view.findViewById(R.id.energyNumber);
        energylayout = view.findViewById(R.id.energylayout);
        setProgressBar(progressBar);

        instalButtomTwoButn.setOnClickListener(this);
    }

    public void setFormId(int formId) {
        this.formId = formId;
    }

    @Override
    public void updateButn(DownLoadTaskTable downloadTask) {
        int status = 0;
        if (downloadTask != null) {
            boolean isInstall = APKUtils.getInstance(getContext()).checkInstall(getContext(), downloadTask.getPackageName());
            DownLoadTaskTable taskDb = DownloadTaskManager.getInstance().findByGameId(downloadTask.getGameId());
            if (isInstall && taskDb == null && downloadTask.getDownStatus() != DownLoadStatusConfig.UPDATE)
                downloadTask.setDownStatus(DownLoadStatusConfig.INSTALLED);
            if (downloadTask != null) status = downloadTask.getDownStatus();

        }
        energylayout.setVisibility(GONE);
        instalButtomButn.setPadding(0, 0, 0, 0);
        instalButtomTwoButn.setPadding(0, 0, 0, 0);
        LogUtils.e("statues  2   " + status + "");
        progressBar.setForm(formId);
        progressBar.setVisibility(GONE);

        if (formId==1) {
//            if (status == DownLoadStatusConfig.NORMAL && MyApplication.downLoadWeekNum > 0
//                    && MyApplication.downLoadDayNum > 0) {
//                moneyText.setText("+ "+ MyApplication.downLoadRard +"元");
//                moneyText.setTextColor(Color.parseColor("#ffffff"));
//                moneyText.setVisibility(VISIBLE);
//            }

            DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
            if (credit_info!=null && credit_info.getDay_surplus()>0
                    && credit_info.getWeek_surplus()>0
                    && credit_info.getGame_down_load_reward()>0
                    && (taskTable==null || taskTable.getDownStatus()==0)) {
                moneyText.setVisibility(VISIBLE);
                moneyText.setTextColor(Color.parseColor("#ffffff"));
                moneyText.setText("+ "+ credit_info.getGame_down_load_reward() +"元");
            } else {
                moneyText.setVisibility(GONE);
            }

            instalButtomButn.setVisibility(GONE);
            instalButtomTwoButn.setVisibility(VISIBLE);
            instalButtomTwoButn.setBackgroundResource(R.drawable.installbutn_detial_normal_two_butn);
            instalButtomTwoButn.setTextColor(Color.WHITE);
            if (status == DownLoadStatusConfig.UPDATE) {
            } else if (status == DownLoadStatusConfig.UNZIP) {
                instalButtomTwoButn.setText("解压中");
                instalButtomTwoButn.setTextColor(Color.parseColor("#4bbd6e"));
                instalButtomTwoButn.setBackgroundResource(R.drawable.gamedetial_unziping_butn);
            } else if (status == DownLoadStatusConfig.UNZIP_ERROR || status == DownLoadStatusConfig.UNZIP_WAIT) {
                instalButtomTwoButn.setText("解压");
                instalButtomTwoButn.setTextColor(Color.WHITE);
                instalButtomTwoButn.setBackgroundResource(R.drawable.gamedetial_installandunzip_butn);
            } else if (status == DownLoadStatusConfig.INSTALL_WAIT) {
                instalButtomTwoButn.setText("安装");
                instalButtomTwoButn.setTextColor(Color.WHITE);
                instalButtomTwoButn.setBackgroundResource(R.drawable.gamedetial_installandunzip_butn);
            } else if (status == DownLoadStatusConfig.INSTALLING) {
                instalButtomTwoButn.setText("安装中");
                instalButtomTwoButn.setTextColor(Color.WHITE);
                instalButtomTwoButn.setBackgroundResource(R.drawable.installbutton_installandunzip_butn);
            } else if (status == DownLoadStatusConfig.DOWNLOAD_FINISH) {
            } else if (status == DownLoadStatusConfig.DOWNLOADING || status == DownLoadStatusConfig.START) {
                setProgress(downloadTask);
            } else if (status == DownLoadStatusConfig.DOWNLOAD_STOP || status == DownLoadStatusConfig.DOWNLOAD_ERROR) {
                setProgress(downloadTask);
            } else if (status == DownLoadStatusConfig.WAIT) {
                instalButtomTwoButn.setText("等待");
            } else if (status == DownLoadStatusConfig.INSTALLED) {
                instalButtomTwoButn.setTextColor(Color.parseColor("#FFFFFF"));
                instalButtomTwoButn.setBackgroundResource(R.drawable.installbutn_detial_normal_two_butn);
                instalButtomTwoButn.setText("打开");
            } else {
//            instalButtomTwoButn.setPadding(getResources().getDimensionPixelOffset(R.dimen.wdp60), 0, 0, 0);
                instalButtomTwoButn.setText("立即下载");
//            energylayout.setVisibility(VISIBLE);
                if (downloadTask != null) {
                    UtilsMy.setButnStatusGamedetial(instalButtomTwoButn, downloadTask.getDownStatus(), formId);
                }
            }
        } else {
            instalButtomTwoButn.setVisibility(GONE);
            instalButtomButn.setVisibility(VISIBLE);
            instalButtomButn.setBackgroundResource(R.drawable.installbutn_detial_normal_butn);
            instalButtomButn.setTextColor(Color.WHITE);
            if (status == DownLoadStatusConfig.UPDATE) {
            } else if (status == DownLoadStatusConfig.UNZIP) {
                instalButtomButn.setText("解压中");
                instalButtomButn.setTextColor(Color.parseColor("#4bbd6e"));
                instalButtomButn.setBackgroundResource(R.drawable.gamedetial_unziping_butn);
            } else if (status == DownLoadStatusConfig.UNZIP_ERROR || status == DownLoadStatusConfig.UNZIP_WAIT) {
                instalButtomButn.setText("解压");
                instalButtomButn.setTextColor(Color.WHITE);
                instalButtomButn.setBackgroundResource(R.drawable.gamedetial_installandunzip_butn);
            } else if (status == DownLoadStatusConfig.INSTALL_WAIT) {
                instalButtomButn.setText("安装");
                instalButtomButn.setTextColor(Color.WHITE);
                instalButtomButn.setBackgroundResource(R.drawable.gamedetial_installandunzip_butn);
            } else if (status == DownLoadStatusConfig.INSTALLING) {
                instalButtomButn.setText("安装中");
                instalButtomButn.setTextColor(Color.WHITE);
                instalButtomButn.setBackgroundResource(R.drawable.installbutton_installandunzip_butn);
            } else if (status == DownLoadStatusConfig.DOWNLOAD_FINISH) {
            } else if (status == DownLoadStatusConfig.DOWNLOADING || status == DownLoadStatusConfig.START) {
                setProgress(downloadTask);
            } else if (status == DownLoadStatusConfig.DOWNLOAD_STOP || status == DownLoadStatusConfig.DOWNLOAD_ERROR) {
                setProgress(downloadTask);
            } else if (status == DownLoadStatusConfig.WAIT) {
                instalButtomButn.setText("等待");
            } else if (status == DownLoadStatusConfig.INSTALLED) {
                instalButtomButn.setTextColor(Color.parseColor("#FFFFFF"));
                instalButtomButn.setBackgroundResource(R.drawable.installbutn_detial_normal_butn);
                instalButtomButn.setText("打开");
            } else {
//            instalButtomButn.setPadding(getResources().getDimensionPixelOffset(R.dimen.wdp60), 0, 0, 0);
                instalButtomButn.setText("立即下载");
//            energylayout.setVisibility(VISIBLE);
                if (downloadTask != null) {
                    UtilsMy.setButnStatusGamedetial(instalButtomButn, downloadTask.getDownStatus());
                }
            }
        }
    }

    public void setEnergyText(String energy) {
        energyNumber.setText(energy);
    }

    public void setProgress(DownLoadTaskTable downLoadTaskTable) {
        if (formId==1) {
            instalButtomTwoButn.setTextColor(Color.parseColor("#999999"));
            instalButtomTwoButn.setBackgroundResource(R.drawable.installbutn_detial_ing_butn);
            instalButtomTwoButn.setVisibility(GONE);
            moneyText.setVisibility(GONE);
        } else {
            instalButtomButn.setTextColor(Color.parseColor("#999999"));
            instalButtomButn.setBackgroundResource(R.drawable.installbutn_detial_ing_butn);
            instalButtomButn.setVisibility(GONE);
        }
//        instalButtomButn.setText(progress + "%");
//        instalButtomButn.setAlpha(0);
        progressBar.setForm(formId);
        progressBar.setVisibility(VISIBLE);
        if (downLoadTaskTable.getDownStatus() == DownLoadStatusConfig.DOWNLOADING
                || downLoadTaskTable.getDownStatus() == DownLoadStatusConfig.START) {
            progressBar.setState(102);

        } else {
            progressBar.setState(103);

        }
        progressBar.setProgress((float) downLoadTaskTable.getDownprogress());

    }
}
